from flask import render_template,redirect,url_for,session, flash
from flask_login import login_required, current_user
#from flaskext.uploads import UploadSet, IMAGES
from bson.dbref import DBRef

from controller import app
from lib.form import Form
from dao.ticket import TicketDao

from service.user import UserForm, UserService
from service.ticket import TicketForm, TicketService


@app.route('/signup_and_booking')
@app.route('/signup_and_booking', methods=['GET','POST'])
def signup_and_booking():
    user_form = UserForm()
    if user_form.is_submitted():
        # Hit Next button will do user validate, so not validate again here
        user_service = UserService()
        _id = user_service.signup(user_form)
        if _id is not None:
            user_service.login(user_form.email, user_form.password)
            
            ticket_form = TicketForm()
            if ticket_form.errors == []:
                ticket_service = TicketService()
                tickets = ticket_service.save_tickets(ticket_form)
                return render_template('user_portal.html', tickets=tickets)
            else:
                pass
            
        else:
            return render_template('index.html', username=None)
    else:
        # Fix me, temp for developing
        return render_template('user_portal.html')

"""
@app.route('/manage_tickets')
@app.route('/manage_tickets', methods=['GET','POST'])
@login_required
def manage_tickets():
    form = TicketForm()
    dao = TicketDao()
    
    if form.is_submitted():
        if form.action == 'edit':
            category = category_dao.update_category(form.id, form.name, form.description)
        else:
            _dict = {'name':form.name,'description':form.description}
            parent = category_dao.find_one({'name':form.parent})
            if parent is not None:
                _dict['parent'] = DBRef('addresses', parent._id)
            else:
                _dict['parent'] = None
            category_dao.save(_dict)
            
            #flash("Image saved.")
            #return redirect(url_for('pictures'))
            return redirect(url_for('display_categories'))
    return redirect(url_for('display_categories'))

@app.route('/display_categories')
@login_required
def display_categories():
    #return render_template("add_category-v1.html", categorys=None)
    
    category_dao = CategoryDao()
    _categories = category_dao.all()
    if len(_categories) == 0:
        return render_template("manage_categories.html", _categories=None)
    else:
        cats = []
        for _category in _categories:
            if _category.parent is not None:
                parent = category_dao.find_one({'_id':_category.parent.id})
                cats.append({"name":_category.name, "description":_category.description, "parent":parent.name})
            else:
                cats.append({"name":_category.name, "description":_category.description, "parent":"None"})
        
        return render_template("manage_categories.html", categories=cats)
"""    